layui.define(["jquery"],function(exports){
    var $ = layui.jquery,
        conf = layui.conf;
    var self = {};
    self.menuCell = 20;// 子菜单偏移像素值
    self.initMenuHtml = function(menuData){
        var data = menuData;
        var liStr = '';
        // 遍历生成主菜单
        $.each(data, function(i, obj){
            // 判断是否存在子菜单
            if(data[i].sub != null && data[i].sub.length > 0){
                liStr += '<li class="layui-nav-item nav-item">';
                liStr += '<a lay-href="javascript:;" lay-tips="' + data[i].alt + '" lay-direction="1" data-id="' + data[i].id + '" data-title="' + data[i].alt + '">';
                liStr += '<i class="layui-icon layui-icon-' + data[i].icon + '"></i>';
                liStr += '<cite>' + data[i].alt + '</cite>';
                liStr += '</a>';
                liStr += '<dl class="layui-nav-child">';
                // 遍历获取子菜单
                for( var k = 0; k < data[i].sub.length; k++){
                    liStr += self.getSubMenu(data[i].sub[k]);
                }
                liStr += "</dl></li>";
            }else{
                liStr += '<li class="layui-nav-item nav-item">';
                liStr += '<a lay-href="javascript:;" lay-tips="' + data[i].alt + '" data-id="' + data[i].id + '" data-title="' + data[i].alt + '">';
                liStr += '<i class="layui-icon layui-icon-' + data[i].icon + '"></i>';
                liStr += '<cite>' + data[i].alt + '</cite>';
                liStr += '</a></li>';
            }
        });
        return '<ul class="layui-nav layui-nav-tree" lay-filter="menu">' + liStr + '</ul>';
    }

    // 递归生成子菜单
    self.getSubMenu = function(childMenu, num=1) {
        num++;
        var childStr = '';
        if(childMenu.sub != null && childMenu.sub.length > 0){
            childStr += '<dd><ul><li class="layui-nav-item">';
            childStr += '<a lay-href="javascript:;" lay-tips="' + childMenu.alt + '" lay-direction="' + num + '" data-id="' + childMenu.id + '" data-title="' + childMenu.alt + '" style="padding-Left:' + num*self.menuCell + 'px"><i class="layui-icon layui-icon-"' + childMenu.icon + '></i><cite>' + childMenu.alt + "</cite></a>";
            childStr += '<dl class="layui-nav-child">';
            for( var j = 0; j < childMenu.sub.length; j++){
                childStr += self.getSubMenu(childMenu.sub[j], num);
            }
            childStr += '</dl></li></ul></dd>';
        }else{
            childStr += '<dd><a lay-href="' + childMenu.url + '" lay-tips="' + childMenu.alt + '" data-id="' + childMenu.id + '" data-title="' + childMenu.alt + '" style="padding-Left:' + num*self.menuCell + 'px"><i class="layui-icon layui-icon-"' + childMenu.icon + '></i><cite>' + childMenu.alt + '</cite></a></dd>';// style="pading-Left:' + num*self.menuCell + 'px"
        }
        return childStr;
    }

    exports("menu",self);
})